package com.wp.service.impl;

import com.jt.mapper.*;
import com.jt.pojo.*;
import com.wp.mapper.*;
import com.wp.pojo.basePojo.BlogResult;
import com.wp.service.BlogService;
import com.wp.pojo.Blog;
import com.wp.pojo.BlogSort;
import com.wp.pojo.PageResult;
import com.wp.pojo.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class BlogServiceImpl implements BlogService {
    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private BlogResultMapper blogResultMapper;
    @Autowired
    private TagMapper tagMapper;
    @Autowired
    private PictureMapper pictureMapper;
    @Autowired
    private BlogSortMapper blogSortMapper;

    /**
     * 博客发布
     */
    @Override
    public void isPublishBlog(Blog blog) {
        Date date = new Date();
        blog.setUpdateTime(date);
        blog.setUpdateTime(date);
        blog.setIsPublish("1");
        blogMapper.isPublishBlog(blog);

    }

    /**
     * 博客删除
     */
    @Override
    public void updateStatus(Blog blog) {
        Date date = new Date();
        blog.setUpdateTime(date);
        blog.setStatus(0);
        blogMapper.updateStatusBlog(blog);
    }

    /**
     * 获取博客列表
     * */
    @Override
    public PageResult getBlogList(PageResult pageResult) {

        String query = pageResult.getQuery();
        Integer size = pageResult.getPageSize();
        Integer startNum = (pageResult.getPageNum()-1)*size;
        Integer status = 1;

        //获取所有满足条件的记录，是一个集合A
         List<BlogResult> blogResultsList= blogResultMapper
                 .getBlogResultList(query,startNum,size,status);
        //获取模糊查询的总记录数
        Long total = blogMapper.getBlogListCount(query,status);
        //为前端所需的集合查找数据 aa bb cc
        //将aa bb cc 集合封进A属性
        for (BlogResult blogResult:blogResultsList) {
            String[] tagUids = blogResult.getTagUid().split(",");
            //拆分为单一的tag后再查其名字
            List<Tag> tagList = new ArrayList<>();
            for (String tagUid: tagUids) {
                Tag tag = tagMapper.getTag(tagUid,status);
                tagList.add(tag);
            }
            blogResult.setTagList(tagList);
            //bb---fileUid对应的图片名字、url
            String fileUid = blogResult.getFileUid();
            String picName = pictureMapper.getPicture(fileUid,status);
            List<String> photoList = new ArrayList<>();
            String baseUrl = "";
            String photoUrl = baseUrl + picName;
            photoList.add(photoUrl);
            photoList.add(picName);
            blogResult.setPhotoList(photoList);
            //cc--blog_sort_uid对应的博客分类名称、
            String blogSortUid = blogResult.getBlogSortUid();
            BlogSort blogSort = blogSortMapper.getBlogSortName(blogSortUid,status);
            blogResult.setBlogSort(blogSort);
        }
        pageResult.setTotal(total).setRows(blogResultsList);

        //将A封进pageResult对象，返回controller
        return pageResult;
    }
    /**
     * 新增博客
     * */
    @Override
    public void addBlog(Blog blog) {
        String uid = UUID.randomUUID().toString().replace("-", "");
        Integer status=1;
        Integer sort=0;
        Integer openComment=0;
        Integer type=0;
        Integer articleSource=0;
        List<Tag> tagUidList = blog.getTagList();
        String tagList="";
        for (Tag tagUid:tagUidList) {
             String taguid= tagUid.getUid();
            tagList =taguid+",";
        }
        tagList= tagList.substring(0,tagList.length()-1);
        blog.setTagUid(tagList);

        Date date = new Date();
        blog.setIsPublish("0").setCreateTime(date).setUpdateTime(date).setStatus(status).setUid(uid);
        blog.setSort(sort).setOpenComment(openComment).setType(type).setArticleSource(articleSource);
        //先判断数据库中是否已经存在该文章，不能重复新增--------先不考虑重复
        //直接新增，存入数据库
        //获取用户---uid
        blogMapper.addBlog(blog);

    }
    /**
     * 编辑博客
     * */
    @Override
    public void editBlog(Blog blog) {
        Date date = new Date();
        blog.setUpdateTime(date);
        List<Tag> tagUidList = blog.getTagList();

        String tagList="";
        for (Tag tagUid:tagUidList) {
            String taguid= tagUid.getUid();
            tagList =taguid+",";
        }
        tagList= tagList.substring(0,tagList.length()-1);
        blog.setTagUid(tagList);
        blogMapper.editBlog(blog);
    }
}
//    @Override
//    public PageResult getBlogList(PageResult pageResult) {
////        BlogResult blogResult=new BlogResult();
//
//        Integer num = pageResult.getPageNum();
//        Integer size = pageResult.getPageSize();
//        String query = pageResult.getQuery().trim();
////        List tagList = blogResult.getTagList();
//        Integer status = 1;
//        Integer startNum = (num -1 )*size;
//        //获取博客查询总数
//        Integer total = blogMapper.getBlogCount(query,status);
//        pageResult.setTotal(total);
//        //非集合属性基类------此时由于表中字段tag_uid、blog_sort_uid与blogResult
//        //对象中的两个集合属性名不一致，导致其属性为null---尝试修改基类属性，
//        //新增两个表中同名String属性
//        List<BlogResult> blogs= blogResultMapper.getBlogResultList(query,status,startNum,size);
//        //取出tag对应属性，查找信息封装成list,再封装到集合属性基类对象上，
//        for (BlogResult blog: blogs) {
//           String tags= blog.getTagUid();
//           String[] taguids = tags.split(",");
//           List<Tag> tagList=new ArrayList<>();
//           //设置标签列表
//            for (String tagid:taguids) {
//                //获取tag的内容content
//              Tag tag= tagMapper.getTag(tagid,status);
////              tagList.add(tagContent);
//              tagList.add(tag);
//            }
//            blog.setTagList(tagList);
//
//        //取出图片对应属性，查找信息封装成list,再封装到集合属性基类对象上
//            List<String> photoList = new ArrayList<>();
//            String fileUid = blog.getFileUid();
//            String photoUrl = pictureMapper.getPicName(fileUid,status);
//            photoList.add(photoUrl);
//           //向photoList属性添加图片url
//           blog.setPhotoList(photoList);
//
//        //取出博客分类属性blogsort，封装uid和sortname后set回基类对象
//
//            String blogSortUid = blog.getBlogSortUid();
//            BlogSort blogSort= blogSortMapper.getBlogSortName(blogSortUid,status);
//            blog.setBlogSort(blogSort);
//
//        }
//        pageResult.setRows(blogs);
//        pageResult.setTotal(total);
//        return pageResult;
//    }
//}
